package vn.demand.dragon.view
{
	import flash.display.DisplayObject;
	import flash.events.Event;
	
	import flexed.widgets.alerts.alert;
	
	import mx.core.Application;
	import mx.managers.PopUpManager;
	
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	import vn.demand.dragon.FFIAApplicationFacade;
	import vn.demand.dragon.model.ProjectFilesProxy;
	import vn.demand.dragon.model.UserProxy;
	import vn.demand.dragon.view.components.ProjectFileUpload;;
	
	public class ProjectFileUploadMediator extends Mediator
	{
		
		public static var NAME:String = "ProjectFileUploadMediator";
		
		private var pfilesProxy:ProjectFilesProxy;
		private var projectVO:Object;
		private var file:Object;
		var userProxy:UserProxy;
		
		public function ProjectFileUploadMediator(mediatorId:String)
		{
			super(mediatorId);
			userProxy = facade.retrieveProxy(UserProxy.NAME) as UserProxy;
			pfilesProxy = facade.retrieveProxy(ProjectFilesProxy.NAME) as ProjectFilesProxy;
		}

		public function get fileForm():ProjectFileUpload{
			return viewComponent as ProjectFileUpload;
		}
		
		public function show(projectVO:Object, file:Object):void {
			this.file = file;
			this.projectVO = projectVO;
			var popup:ProjectFileUpload = PopUpManager.createPopUp(Application.application as DisplayObject, ProjectFileUpload, true) as ProjectFileUpload;
			setViewComponent(popup);
			fileForm.addEventListener('file_uploaded', onFileUploaded);
			
			if (Number(file.pfile_id) > 0) {
				fileForm.title = "Edit file: " + file.pfile_name;
			} else {
				fileForm.title = "Add file";
			}
			fileForm.sessionId = userProxy.userInfo.sessionid;
			fileForm.projectId = projectVO.project_id;
			fileForm.addEventListener("addFile", onAddFile);
			fileForm.addEventListener("cancelAddFile", onClose);
			PopUpManager.centerPopUp(popup);
		}
		
		private function onFileUploaded(event:Event):void {
			sendNotification(FFIAApplicationFacade.RELOAD_PFILES, projectVO, String(projectVO.project_id));
		}
		
		public function onAddFile(event:Event):void {
			if (!doValidate()) {
				alert.error(errorStrings);
				return;
			}
			pfilesProxy.saveProjectFiles(file);
		}
		
		public function doValidate():Boolean {
			errorStrings = "";
//			if (StringUtil.trim(fileForm.code.text).length == 0) {
//				errorStrings += "Please input [Customer Code]\n";
//			}
//			if (StringUtil.trim(fileForm.com_name.text).length == 0) {
//				errorStrings += "Please input [Customer Name]\n";
//			}
//			if (fileForm.com_status.selectedIndex == 0) {
//				errorStrings += "Please select [Status]\n";
//			}
//			if (fileForm.type.selectedIndex == 0) {
//				errorStrings += "Please select [Type]\n";
//			}
			return errorStrings.length == 0	? true : false;
		}
		
		public function onClose(event:Event):void {
			if (null != fileForm) {
				PopUpManager.removePopUp(fileForm);
			}
		}
		
		override public function listNotificationInterests():Array 
        {
            return [ 
            	FFIAApplicationFacade.PROJECT_FILE_UPDATED,
            	FFIAApplicationFacade.PROJECT_FILE_UPDATE_FAILED,
					 ];
        }
        
        override public function handleNotification( note:INotification ):void 
        {
            switch ( note.getName() ) 
			{
				case FFIAApplicationFacade.PROJECT_FILE_UPDATED:
					onClose(new Event("closeProjectFileUpload"));
					break;
				case FFIAApplicationFacade.PROJECT_FILE_UPDATE_FAILED:
					errorStrings = note.getBody() as String;
					alert.error(errorStrings);
                default:
                	break;
            }
        }
        
        private var errorStrings:String;
	}
}